package com.security.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * 发送rpc请求的工具类
 *
 * @author 大忽悠
 * @create 2022/11/28 10:18
 */
@Slf4j
public class RpcUtil {
    private static final RestTemplate REST_TEMPLATE = new RestTemplate();

    /**
     * 表单形式提交请求
     */
    public static <T> T postParams(String url, Map<String,Object> params,T resType){
        return (T) REST_TEMPLATE.postForObject(url,buildMultiValueMapFromMap(params),resType.getClass());
    }

    public static <T> T postObj(String url, Object requestObj,T resType){
        return (T) REST_TEMPLATE.postForObject(url,requestObj,resType.getClass());
    }

    /**
     * 占位符参数
     */
    public static void delUrlVariable(String url,String param){
        REST_TEMPLATE.delete(url,param);
    }

    private static MultiValueMap buildMultiValueMapFromMap(Map<String,Object> params) {
          MultiValueMap paramsMap=new LinkedMultiValueMap();
          params.forEach(paramsMap::add);
          return paramsMap;
    }
}
